<?php TPL::output('global/header_meta.tpl.htm'); ?>
<style type="text/css">
.oldPwdTip {
    position: absolute;
    color: rgba(153, 153, 153, 1);
    margin-top: 6px;
    margin-left: 0px;
    text-align: left;
    left: 0px;
    top: -5px;
    padding: 0 15px;
    width: 100%;
    height: 40px;
    line-height: 38px;
    color: #777777;
}  
</style>
<div class="aw-register-box">
    <div class="mod-head">
        <a href=""><img src="<?php echo G_STATIC_URL; ?>/css/<?php echo $this->template_name; ?>/img/login_logo.png" alt="" /></a>
        <h1><?php _e('注册新用户'); ?></h1>
    </div>
    <div class="mod-body">
        <form class="aw-register-form" action="account/ajax/register_process/type-<?php echo $this->type?>" method="post" id="register_form">
            <?php if ($this->icode) { ?><input type="hidden" name="icode" id="icode" value="<?php echo $this->icode; ?>" /><?php } ?>
            <?php if ($this->return_url) { ?><input type="hidden" name="return_url" value="<?php echo $this->return_url; ?>" /><?php } ?>

            <ul>
                <li class="alert alert-danger collapse error_message text-left">
                    <i class="icon icon-delete"></i> <em></em>
                </li>
                <li>
                    <input class="aw-register-name form-control" type="text" name="user_name" placeholder="<?php _e('用户名'); ?>" tips="<?php echo '请输入一个'.intval(get_setting('username_length_min')).'-'.intval(get_setting('username_length_max')).' 位的用户名';?>" errortips="<?php _e('用户名长度不符合');?>" value="" />
                </li>

                <?php if($this->type == 'mobile'){?>
                    <li>
                        <input class="aw-register-email form-control" id="phone" name="mobile" type="text" placeholder="手机号" tips="<?php _e('请输入你正在使用的手机号'); ?>" value="<?php echo htmlspecialchars($_GET['mobile']); ?>"  errortips="<?php _e('手机号不正确'); ?>" >
                        
                    </li>
                    <li class="aw-register-verify">
                        <div class="embed-captcha"></div>
                        <p class="wait">正在加载验证码......</p>
                    </li>
                    <li>
                        <input class="aw-register-email form-control" style="width: 170px;margin-right: 20px;display: inline-block;" name="smscode" type="text"  placeholder="短信验证码" tips="<?php _e('请准确输入收到的短信验证码'); ?>" errortips="<?php _e('短信验证码不正确'); ?>" ><button id="Smsbtn" type="button"   style="width: 120px;position:relative; top:-2px;  display: inline-block;"  class="btn btn-large btn-blue btn-block">获取验证码</button>
                    </li>
                <?php } else {?>
                    <li>
                        <input class="aw-register-email form-control" type="text" placeholder="<?php _e('邮箱'); ?>" name="email" tips="<?php _e('请输入你常用的电子邮箱作为你的账号'); ?>" value="<?php echo htmlspecialchars($_GET['email']); ?>" errortips="<?php _e('邮箱格式不正确'); ?>" />
                    </li>
                <?php }?>

                <li>
                    <label class="oldPwdTip aw-register-pwd-label" onclick="$(this).hide();$('#aw-register-pwd').focus();"><?php _e('密码'); ?></label>
                    <input class="aw-register-pwd form-control" id='aw-register-pwd' type="password" tips="<?php _e('请输入 6-16 个字符,区分大小写'); ?>" errortips="<?php _e('密码不符合规则'); ?>" onfocus="hideOldPwdTip('aw-register-pwd');" onblur="toggleOldPwdTip('aw-register-pwd',this.value);"/>
                    <input id="aw-login-user-repassword" type="hidden" name="password" />
                </li>
                <li class="more-information collapse">
                    <ul>
                        <li>
                            <?php _e('性别'); ?>:
                            <label>
                                <input name="sex" value="1" type="radio" /> <?php _e('男'); ?>
                            </label>&nbsp;
                            <label>
                                <input name="sex" value="2" type="radio" /> <?php _e('女'); ?> </label>&nbsp;
                            <label>
                                <input name="sex" value="3" type="radio" checked="checked" /> <?php _e('保密'); ?>
                            </label>
                        </li>
                        <li>
                            <?php _e('职业'); ?>:
                            <select name="job_id">
                                <option value="">--</option>
                                <?php echo H::display_options($this->job_list); ?>
                            </select>
                        </li>
                        <li>
                            <?php _e('所在城市'); ?>:
                            <select name="province" class="select_area" style="display:inline-block"></select>

                            <select name="city" class="select_area"></select>
                        </li>
                        <li>
                            <input type="text" class="form-control" placeholder="<?php _e('一句话介绍'); ?>" id="welcome_signature" value="<?php if ($this->user_info['signature']) { echo $this->user_info['signature']; } ?>" name="signature" />
                        </li>
                    </ul>
                </li>
                <li>
                    <hr />
                    <a class="more-information-btn"><?php _e('更多资料'); ?></a>
                </li>
                <?php if (get_setting('register_seccode') == 'Y' and $this->type=='email') { ?>
                <li class="aw-register-verify">
                    <div class="embed-captcha"></div>
                    <p class="wait">正在加载验证码......</p>
                </li>
                <!-- <li class="aw-register-verify">
                    <img class="pull-right" id="captcha" onclick="this.src = G_BASE_URL + '/account/captcha/' + Math.floor(Math.random() * 10000);" src="">
                
                    <input type="text" class="form-control" name="seccode_verify" placeholder="<?php _e('验证码'); ?>" />
                </li> -->
                <?php } ?>
                <li class="last">
                    <label><input type="checkbox" checked="checked" value="agree" name="agreement_chk" /> <?php _e('我同意'); ?></label>
                    <a href="javascript:;" class="aw-agreement-btn"><?php _e('用户协议'); ?></a>
                    <a href="account/login/" class="pull-right">已有账号?</a>
                    <!-- <a style="margin-right:8px;" href="account/register/type-email" class="pull-right">邮箱注册</a> -->
                    <?php if($this->other_valid){?>
                        <a href="account/register/type-<?php echo $this->other_valid['type']?>" class="pull-right" style="margin-right: 5px;"><?php echo $this->other_valid['name'] ?></a>
                    <?php }?>
                    <div class="aw-register-agreement collapse">
                        <div class="aw-register-agreement-txt" id="register_agreement"></div>
                    </div>
                </li>
                <li class="clearfix">
                    <button class="btn btn-large btn-blue btn-block" id="zhuce" <?php if ((get_setting('register_seccode') == 'N' and $this->type=='email') || $this->type=='mobile') { ?> onclick=" $('#aw-login-user-repassword').val(encrypt_pass($('#aw-register-pwd').val()));AWS.ajax_post($('#register_form'), AWS.ajax_processer, 'error_message'); return false;" <?php } ?> ><?php _e('注册'); ?></button>
                </li>
            </ul>
        </form>
    </div>
    <div class="mod-footer"></div>
</div>
<script type="text/javascript">
 $(".aw-register-pwd").prev(".oldPwdTip").click(function(){
    $(".aw-register-pwd").focus();
});  
 $(".aw-register-pwd").focus(function(){
    $(".oldPwdTip").hide();
});
 $(".aw-register-pwd").blur(function(){
    if(!$(this).val())
    $(".oldPwdTip").show();
});
var mobile;
$(document).ready(function ()
{
    $.get(G_BASE_URL + '/account/ajax/register_agreement/', function (result) { $('#register_agreement').html(result.err); }, 'json');

    $('.aw-agreement-btn').click(function()
    {
        if ($('.aw-register-agreement').is(':visible'))
        {
            $('.aw-register-agreement').hide();
        }
        else
        {
            $('.aw-register-agreement').show();
        }
    });

    $('.more-information-btn').click(function()
    {
        $('.more-information').fadeIn();
        $(this).parent().hide();
    });

    verify_register_form('#register_form');

    /* 注册页面验证 */
    function verify_register_form(element)
    {

        $(element).find('[type=text], [type=password]').on({
            focus : function()
            {
                if (typeof $(this).attr('tips') != 'undefined' && $(this).attr('tips') != '')
                {
                    $(this).parent().append('<b class="aw-reg-tips">' + $(this).attr('tips') + '</b>');
                }
            },
            blur : function()
            {
                if ($(this).attr('tips') != '')
                {
                    switch ($(this).attr('name'))
                    {
                        case 'user_name' :
                            var _this = $(this);
                            var min_length=<?php echo intval(get_setting('username_length_min'));?>;
                            var max_length=<?php echo intval(get_setting('username_length_max'));?>;
                            $(this).parent().find('.aw-reg-tips').detach();
                            if (strlen($(this).val()) >= 0 && strlen($(this).val())< min_length)
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            if (strlen($(this).val()) > max_length)
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            else
                            {
                                $.post(G_BASE_URL + '/account/ajax/check_username/', 
                                    {
                                        username: $(this).val()
                                    }, function (result)
                                {
                                    if (result.errno == -1)
                                    {
                                        _this.parent().find('.aw-reg-tips').detach();
                                        _this.parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + result.err + '</b>');
                                    }
                                    else
                                    {
                                        _this.parent().find('.aw-reg-tips').detach();
                                        _this.parent().append('<b class="aw-reg-tips aw-reg-right"><i class="aw-icon i-followed"></i></b>');
                                    }
                                }, 'json');
                            }
                            return;

                        case 'email' :
                            $(this).parent().find('.aw-reg-tips').detach();
                            var emailreg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
                            if (!emailreg.test($(this).val()))
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            else
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-right"><i class="aw-icon i-followed"></i></b>');
                            }
                            return;

                        case 'password' :
                            $(this).parent().find('.aw-reg-tips').detach();
                            if ($(this).val().length >= 0 && $(this).val().length < 6)
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            if ($(this).val().length > 17)
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            else
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-right"><i class="aw-icon i-followed"></i></b>');
                            }
                            return;

                        case 'mobile':
                            $(this).parent().find('.aw-reg-tips').detach();
                            var phonereg = /^1[345789]\d{9}$/;
                            if (!phonereg.test($(this).val()))
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            else
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                            }
                            return;

                        case 'smscode':
                            $(this).parent().find('.aw-reg-tips').detach();
                            if (!$(this).val())
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                                $(this).parent().append('<b class="aw-reg-tips aw-reg-err"><i class="aw-icon i-err"></i>' + $(this).attr('errortips') + '</b>');
                                return;
                            }
                            else
                            {
                                $(this).parent().find('.aw-reg-tips').detach();
                            }
                            return;

                    }
                }

            }
        });
    }

    //判断字符长度
     function strlen(str){  
        var len = 0;  
        for (var i=0; i<str.length; i++) {   
         var c = str.charCodeAt(i);   
        //单字节加1   
         if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {   
           len++;   
         }   
         else {   
          len+=2;   
         }   
        }   
        return len;  
    }

    $('.select_area').LocationSelect({
        labels: ["<?php _e('请选择省份或直辖市'); ?>", "<?php _e('请选择城市'); ?>"],
        elements: document.getElementsByTagName("select"),
        detector: function () {
            this.select(["<?php echo $this->user_info['province']; ?>", "<?php echo $this->user_info['city']; ?>"]);
        },
        dataUrl: G_BASE_URL.replace('/?', '') + '/static/js/areas.js'
    });


    /*仿刷新：检测是否存在cookie*/
    if($.cookie("captcha")){
        var count = $.cookie("captcha");
        var btn = $('#Smsbtn');
        $('#Smsbtn').prop('disabled', true);
        btn.text(count+'秒');
        var resend = setInterval(function(){
            count--;
            if (count > 0){
                btn.text(count+'秒');
                $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
            }else {
                $('#Smsbtn').prop('disabled', false);
                clearInterval(resend);
                btn.text("获取验证码")
            }
        }, 1000);
    }

    var handlerEmbed = function (captchaObj) {
        $("#Smsbtn").click(function (e) {
            var phonereg = /^1[3456789]\d{9}$/;
            var mobile = $('input[name="mobile"]').val();

            if(mobile=='' ){
                AWS.alert("请输入手机号");
                return false;
            }
            if(!phonereg.test(mobile)){
                AWS.alert("手机号格式不正确");
                return false;
            }
            if($('#Smsbtn').text()!="获取验证码"){
                return false;
            }

            var validate = captchaObj.getValidate();
            if (!validate) {
                AWS.alert("请先完成验证");
                return false;
            } else{
                $.post(G_BASE_URL + '/account/ajax/check_mobile/',{
                    mobile:mobile,
                    type:'regist',
                }, function (result)
                {
                    if (result.errno == -1)
                    {
                        AWS.alert("手机号已经注册！");
                        captchaObj.reset();
                        return false;
                    }else{
                        $.ajax({
                            url: G_BASE_URL + '/tools/sendSms/',
                            type: "post",
                            dataType: "json",
                            data: {
                                mobile: mobile,
                                challenge: '',
                                geetest_challenge: validate.geetest_challenge,
                                geetest_validate: validate.geetest_validate,
                                geetest_seccode: validate.geetest_seccode,
                                client_type: 'web',
                                genre: '1'
                            },
                            success: function (result) {
                                if(result.errno == -1)
                                {
                                    AWS.alert(result.err);
                                    captchaObj.reset(); // 调用该接口进行重置 
                                    return false;
                                }else
                                {
                                    AWS.alert(result.err);
                                    var count = 60;
                                    var inl = setInterval(function () {
                                        $('#Smsbtn').prop('disabled', true);
                                        count -= 1;
                                        var text = count + ' 秒';
                                        $.cookie("captcha", count, {path: '/', expires: (1/86400)*count});
                                        $('#Smsbtn').text(text);
                                        if (count <= 0) {
                                            clearInterval(inl);
                                            $('#Smsbtn').prop('disabled', false);
                                            $('#Smsbtn').text('获取验证码');
                                        }
                                    }, 1000);
                                    return true;
                                }

                            }
                        });
                    }
                }, 'json');
            }
        });
    <?php if (get_setting('register_seccode') == 'Y' and $this->type=='email') { ?>
        $('#zhuce').click(function(){
            var validate = captchaObj.getValidate();
            if (!validate) {
                AWS.alert("请先完成验证");
                return false;
            } else{
                $('#aw-login-user-repassword').val(encrypt_pass($('#aw-register-pwd').val()));
                AWS.ajax_post($('#register_form'), AWS.ajax_processer, 'error_message');
                captchaObj.reset(); // 调用该接口进行重置 
                return false;
            }
        });
    <?php } ?>
    <?php if ((get_setting('register_seccode') == 'Y' and $this->type=='email') || $this->type=='mobile') { ?>
        // 将验证码加到id为captcha的元素里，同时会有三个input的值：geetest_challenge, geetest_validate, geetest_seccode
        captchaObj.appendTo(".embed-captcha");
        captchaObj.onReady(function () {
            $(".wait").css('display','none');
        });
    <?php } ?>
    };

    $.ajax({
        // 获取id，challenge，success（是否启用failback）
        url: G_BASE_URL+ "/account/slide_captcha/?t=" + (new Date()).getTime()+"&client_type='web'", // 加随机数防止缓存
        type: "get",
        dataType: "json",
        success: function (data) {
            var dataObj = eval('(' + data.rsm + ')');
            // 使用initGeetest接口
            // 参数1：配置参数
            // 参数2：回调，回调的第一个参数验证码对象，之后可以使用它做appendTo之类的事件
            initGeetest({
                gt: dataObj.gt,
                challenge: dataObj.challenge,
                new_captcha: dataObj.new_captcha,
                offline: !dataObj.success, // 表示用户后台检测极验服务器是否宕机，一般不需要关注
                product: "embed", // 产品形式，包括：float，embed，popup。注意只对PC版验证码有效
                width: '100%' // 设置按钮宽度, 与父元素宽度一致
            }, handlerEmbed);
        }
    });
});
</script>

<?php TPL::output('global/footer.tpl.htm'); ?>
